package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.User;
import com.tongzhan.entity.religion.Activity;
import com.tongzhan.service.ActivityService;
import com.tongzhan.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    private UserService userService;

    @Resource
    private ActivityService activityService;

    /**
     * 上传活动接口
     *
     * @param userId 用户id
     * @param streetCode 街道编码
     * @param siteCode 场所编码
     * @param title 活动主题
     * @param content 活动详细内容
     * @param number 活动人数
     * @param startTime 活动开始时间
     * @param endTime 活动结束时间
     * @param picture 照片
     * @return Return
     */
    @PostMapping("/uploadActivity")
    public Return uploadActivity(String userId, String streetCode, String siteCode, String title, String content,
                                 Long number, String startTime, String endTime, String picture) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(streetCode) || StringUtils.isEmpty(siteCode)
               || StringUtils.isEmpty(title) || "".equals(title.trim()) || StringUtils.isEmpty(startTime)
               || StringUtils.isEmpty(endTime) || number == null) {
           return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (number.toString().length() > 10) {
           return new Return(Return.FAIL, "活动人数过大");
        }
        if (StringUtils.isNotEmpty(content) && content.trim().length() > 5000) {
           return new Return(Return.FAIL, "详细内容过长");
        }
        //用户信息
        User user = userService.selectByUserId(userId);
        if (user == null) {
            return new Return(Return.FAIL, "用户不存在");
        }
        //日期解析
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start;
        LocalDate end;
        try {
            start = LocalDate.parse(startTime, dtf);
            end = LocalDate.parse(endTime, dtf);
        } catch (Exception e) {
            return new Return(Return.FAIL, "日期格式不正确");
        }
        Activity activity = new Activity();
        activity.setUserName(user.getUserName());
        activity.setRealName(user.getRealName());
        activity.setStreetCode(streetCode);
        activity.setSiteCode(siteCode);
        activity.setTitle(title.trim());
        if (StringUtils.isNotEmpty(content)) {
            activity.setDetail(content.trim());
        }
        activity.setNumber(number);
        activity.setStartTime(start);
        activity.setEndTime(end);
        activity.setPicture(picture);
        return activityService.uploadActivity(activity);
    }

    /**
     * 获取活动列表接口
     *
     * @param userId 用户id
     * @param siteCode 场所编码
     * @param religionCode 团体编码
     * @param time 筛选时间
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getActivityList")
    public Return getActivityList(String userId, String siteCode, String religionCode, String time, Integer page,
                                  @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return activityService.getActivityList(userId, siteCode, religionCode, time, page, size);
    }

    /**
     * 活动详情接口
     *
     * @param activityCode 活动编码
     * @return Return
     */
    @PostMapping("/activityDetails")
    public Return activityDetails(String activityCode) {
        if (StringUtils.isEmpty(activityCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return activityService.activityDetails(activityCode);
    }

    /**
     * 删除活动接口
     *
     * @param activityCode 活动编码
     * @return Return
     */
    @PostMapping("/deleteActivity")
    public Return deleteActivity(String activityCode) {
        if (StringUtils.isEmpty(activityCode)) {
            return new Return(Return.FAIL, "参数缺失");
        }
        return activityService.deleteActivity(activityCode);
    }

    /**
     * 修改活动
     *
     * @param activityCode 活动编码
     * @param title 活动主题
     * @param content 活动详细内容
     * @param number 活动人数
     * @param startTime 活动开始时间
     * @param endTime 活动结束时间
     * @param picture 照片
     * @return Return
     */
    @PostMapping("/update")
    public Return update(String activityCode, String title, String content,
                         Long number, String startTime, String endTime, String picture) {
        if (StringUtils.isEmpty(activityCode) || StringUtils.isEmpty(title) || "".equals(title.trim())
                || StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime) || number == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (number.toString().length() > 10) {
            return new Return(Return.FAIL, "活动人数过大");
        }
        if (StringUtils.isNotEmpty(content) && content.trim().length() > 5000) {
            return new Return(Return.FAIL, "详细内容过长");
        }
        return activityService.update(activityCode, title, content, number, startTime, endTime, picture);
    }
}
